package Classes.GameUtils
{
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	/** 
	 * @author thy
	 * @version 1.0.0 
	 * 创建时间：May 2, 2012 4:51:26 PM 
	 */ 
	public class GU_dealBgSound
	{
		private static var _instance:GU_dealBgSound;
		private var _turnMusicByCharacter:int=-1;/*根据人物在地图上面的位置来控制背景音乐*/
		private var _zhuiJiZombieNum:int=0;
		private var _isCanPalyMusic:Boolean=false;
		public function GU_dealBgSound(){	}
		
		public static function get instance():GU_dealBgSound
		{
			if (!_instance)
				_instance=new GU_dealBgSound;
			return _instance;
		}
		
		
		/**
		 *  根据人物在地图上面的位置来控制背景音乐 （0=在家;1=在家外;2=被僵尸追击）
		 */
		public function get turnMusicByCharacter():int
		{
			return _turnMusicByCharacter;
		}
		
		/**
		 * @private
		 */
		public function set turnMusicByCharacter(value:int):void
		{
//			trace("现在的="+value+" 以前的="+turnMusicByCharacter);
			if(turnMusicByCharacter!=value && this.isCanPalyMusic)
			{
				_turnMusicByCharacter = value;
				removeMusicTimer();
				if(value==0)
				{
					//					removeMusicTimer();
					GU_Sound.instance.playBgSound("Back_Refuge",5000,0.2);
				}
				if(value==1)
				{
					//					removeMusicTimer();
					GU_Sound.instance.playBgSound("Back_Town",0.3);
					randomOutHomeMusic();
				}
				
				if(value==2)
				{
					//					removeMusicTimer();
					GU_Sound.instance.playBgSound("Back_Boss",0.2);
					randomOutHomeMusic();
				}
			}
			
		}
		private var musicTimer:Timer;
		private function randomOutHomeMusic():void
		{
			musicTimer=new Timer(10000);
			this.musicTimer.addEventListener(TimerEvent.TIMER,randomMusicFun);
			this.musicTimer.start();
		}
		
		private function randomMusicFun(e:Event):void
		{
			var isPlayFlag:int=Math.floor(Math.random()*2);
			if(isPlayFlag==1)
			{
				var musicNameIndex:int=Math.floor(Math.random()*3)+1;
				var musicName:String="Back_Voice_"+musicNameIndex;
				GU_Sound.instance.playCommSound(musicName);
			}
		}
		
		private function removeMusicTimer():void
		{
			if(null==this.musicTimer)return;
			musicTimer.removeEventListener(TimerEvent.TIMER,randomMusicFun);
			musicTimer.stop();
			musicTimer=null;
		}

		/**
		 * 追击僵尸的个数 
		 * @return 
		 * 
		 */		
		public function get zhuiJiZombieNum():int
		{
			return _zhuiJiZombieNum;
		}

		public function set zhuiJiZombieNum(value:int):void
		{
			if(value<0)
			{
				value=0;
			}
			_zhuiJiZombieNum = value;
		}

		public function get isCanPalyMusic():Boolean
		{
			return _isCanPalyMusic;
		}

		public function set isCanPalyMusic(value:Boolean):void
		{
			_isCanPalyMusic = value;
			if(value)
			{
//				GU_Sound.instance.playBgSound("Back_Refuge",5000,0.2);
				GU_dealBgSound.instance.turnMusicByCharacter=0;
			}
		}


	}
}
